<template>
  <div class="send-message">
    <el-dialog
      title="发送私信"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      width="800px"
      @close="resetForm('form')"
    >
      <div class="sendMsg">
        <div>
          <el-form ref="form" :rules="rules" :model="form" label-width="80px">
            <el-form-item label="标题" prop="title">
              <el-input v-model.trim="form.title"></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <UEditor
                ref="ueditor"
                :ueWidth="680"
                :defaultMsg="form.content"
              />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { sysNoticeAdd } from "@/api/admin/system/sysNotice.js";
import Bus from "@/utils/bus.js";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        title: "",
        content: "",
        receivePerson: [], //消息接收人[1,2,3]
        type: 2,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  mounted() {
    Bus.$on("sendMessage", (v) => {
      if (!v) return;
      this.form.receivePerson = v;
      this.dialogVisible = true;
    });
  },
  beforeDestroy() {
    Bus.$off("sendMessage");
  },
  methods: {
    submitForm(form) {
      this.form.content = this.$refs.ueditor.message;
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.add();
        }
      });
    },
    // 发送私信
    add() {
      sysNoticeAdd(this.form).then((res) => {
        if (res.data.code == 0) {
          this.dialogVisible = false;
          this.$message({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields();
      this.form.title = "";
      this.form.content = "";
      this.form.type = 2;
      this.form.receivePerson = [];
    },
  },
};
</script>